package milktea.servlet.product;

import milktea.entity.PRODUCT;
import milktea.service.PRODUCTService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/manage/admin_doproductadd")
public class DoProductAdd extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
//        resp.setCharacterEncoding("UTF-8");
        PRODUCT p = new PRODUCT();
        if (ServletFileUpload.isMultipartContent(req)) {
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            servletFileUpload.setHeaderEncoding("UTF-8");
            try {
                List<FileItem> list = servletFileUpload.parseRequest(req);
                for (FileItem fileItem : list) {
                    if (fileItem.isFormField()) {
                        String fieldName = fileItem.getFieldName();                  //????? name
                        String fieldValue = fileItem.getString("UTF-8");    //???? value
                        if ("productName".equals(fieldName)) {
//                            p.setPRODUCT_NAME(new String(fieldValue.getBytes(), "UTF-8"));
                            p.setPRODUCT_NAME(fieldValue);
                        }
                        if ("parentId".equals(fieldName)) {
                            p.setPRODUCT_FID(Integer.parseInt(fieldValue.split("-")[0]));
                            p.setPRODUCT_CID(Integer.parseInt(fieldValue.split("-")[1]));
                        }
                        if ("productPrice".equals(fieldName)) {
                            p.setPRODUCT_PRICE(Integer.parseInt(fieldValue));
                        }
                        if ("productDesc".equals(fieldName)) {
//                            p.setPRODUCT_DESCRIPTION(new String(fieldValue.getBytes(), "UTF-8"));
                            p.setPRODUCT_DESCRIPTION(fieldValue);
                        }
                        if ("productStock".equals(fieldName)) {
                            p.setPRODUCT_STOCK(Integer.parseInt(fieldValue));
                        }
                    } else {
//                        System.out.println("????name?????"+fileItem.getFieldName());
                        p.setPRODUCT_FILENAME(fileItem.getName());
                        p.setPRODUCT_ID(0);
                        String path = "D:\\java\\milktea_shop\\web\\images\\";
                        fileItem.write(new File(path + fileItem.getName()));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int count = PRODUCTService.insert(p);
        if (count > 0) {

            resp.sendRedirect("admin_productselect");
        } else {
            PrintWriter out = resp.getWriter();

            out.write("<script>");
            out.write("alert('用户登录失败')");
            out.write("location.href='manage/admin_toproductadd'");
            out.write("</script>");

        }
    }
}
